#include "drv_sleep.h"
#include "public_datas.h"

#define TAG "SLEEP"

static const char *list_wake_rasont[] = {
    "WAKE_BY_POWER_ON",
    "WAKE_BY_BTN",
    "WAKE_BY_USB",
    "WAKE_BY_RTC",
    "WAKE_BY_SF32",
    "WAKE_BY_DEBUG",
};

void rtc_wake_cb()
{
    public_datas()->wake_rason = WAKE_BY_RTC;
}

void drv_sleep()
{
    esay_logi(TAG, "sleep");
    topic_messenger_msg("sleep", NULL, 0);
    uart_wait_tx_done(UART_DEBUG_CH);
    uart_wait_tx_done(UART_SF32_CH);
    public_datas()->sleep_state = ENABLE;
}

void drv_wake()
{
    static uint8_t cnt = 0;
    public_datas()->sleep_state = DISABLE;
    PWR_DCDCCfg(ENABLE);
    esay_logi(TAG, "wake %d", cnt++);
    esay_logi(TAG, "%s", list_wake_rasont[public_datas()->wake_rason]);
    topic_messenger_msg("wakeup", &public_datas()->wake_rason, sizeof(public_datas()->wake_rason));
}

void drv_sleep_install()
{
    PWR_DCDCCfg(ENABLE); // 使能DCDC
}
